import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $: any;
@Component({
	selector: 'sa-transport-platform-link',
	templateUrl: './transport-platform-link.component.html',
	styleUrls: ['./transport-platform-link.component.css']
})
export class TransportPlatformLinkComponent implements OnInit {
	tableData: any = []; // table的表单数据
	totalCounts: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	activeSelected: string = ''; //row高亮
	// 查询参数
	searchKey:any=""; // 用户的搜索关键字

	// 是编辑还是新增的
	formType: any = 'add';

	platformName: string = ''; //平台名称
	platformNameList:any = []; // 平台名称列表
	userName: string = ''; //负责人
	userNameList:any = []; // 负责人列表

	// 表单验证
	validatorOptions = {
		excluded: [':disabled'],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			projectName: {
				validators: {
					notEmpty: {
						message: '请填写平台名称'
					},
				}
			},
			link: {
				validators: {
					notEmpty: {
						message: '请填写平台链接'
					},
				}
			},
		}
	}
	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:TransportPlatformLink')));
		if (!btnData) {
			return
		};
	}
	load() {
		let param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				searchKey:this.searchKey,
				projectName: this.platformName,
				principal: this.userName
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("transportProjectLink/list.html", reqdata).subscribe(result => {
			let data = []
			if (result.isSuccess && result.exhibitDatas) {
				data = result.exhibitDatas
				this.tableData = result.exhibitDatas
				this.totalCounts = result.recordCount;
			}
		});
		this.initGetList();
		this.initGetList(true);
	}
	// 获取下拉选项列表
	initGetList(type?:boolean){
		this.jsonApiService.http("transportProjectLink/initQueryConditions.html",type?'type='+type:{}).subscribe(data=> {
			if(data.success){
				if(type){
					this.userNameList = data.data;
				}else{
					this.platformNameList = data.data;
				}
			}
		})
	}
	// 分页回调
	paginate(e) {
		this.activeSelected = '';
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 刷新表
	refresh() {
		this.load();
	}
	// 删除续费记录
	del_domCode(row) {
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除？').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("transportProjectLink/delete.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	// 表单查询
	tableSearch() {
		this.curPage = 1;
		this.load();
	}


	// 配置提交对象
	transportPlatformLinkObj: any = {
		projectName:"",
		link:"",
		principal:"",
	};
	//

	// 添加的弹窗
	showAddLimitModal(modal) {
		this.formType = 'add'
		modal.show();
		this.resetForm();
	}
	// 重置添加表
	resetForm() {
		this.transportPlatformLinkObj = {
			projectName:"",
			link:"",
			principal:"",
		};
		//初始化表单
		$("#transportPlatformLink").data('bootstrapValidator').resetForm();
	}
	// 关闭添加弹框
	closetransportPlatformLinkModal(modal) {
		$("#transportPlatformLink").data('bootstrapValidator').resetForm();
		modal.hide();
	}

	// 提交添加结果
	submitModal(modal) {
		$('#transportPlatformLink').bootstrapValidator('validate'); //开启验证
		let isValid = $('#transportPlatformLink').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			let reqdata = this.transportPlatformLinkObj
			this.jsonApiService.http_obj("transportProjectLink/modify.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					//
					this.load();
					this.closetransportPlatformLinkModal(modal)
				}
			})
		}
	}
	// 编辑
	editRow(row, modal) {
		modal.show();
		setTimeout(() => {
			this.formType = 'edit'
			this.transportPlatformLinkObj = JSON.parse(JSON.stringify(row))
		}, 300);
	}

}